package com.dashunzi.yueyutianqi.pager;

import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.dashunzi.yueyutianqi.Bean.SmileBean;
import com.dashunzi.yueyutianqi.MainActivity;
import com.dashunzi.yueyutianqi.R;
import com.dashunzi.yueyutianqi.base.BasePager;
import com.dashunzi.yueyutianqi.util.HttpCallbackListener;
import com.dashunzi.yueyutianqi.util.HttpUtil;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.maxwin.view.XListView;


/**
 * Created by Administrator on 2016/3/10 0010.
 * 作者：大顺子
 */
public class smilePager extends BasePager {
    public static final String SMILE_QUEUE = "http://japi.juhe.cn/joke/content/list.from?key=e42ac788527b68a0d367507951260b4a&page=";
    private int page = 1;//笑话页数
    private View mView;
    private XListView mXListView;
    private SmileAdapter mSmileAdapter;
    private boolean isLoadMore;//是否是上拉加载更多
    /**
     * 记录当前的页面
     */
    private int currentPager;
    private long mPreUpdateTime;//更新的时间
    private List<SmileBean.ResultEntity.DataEntity> mSmileBeanList = new ArrayList<>();

    public smilePager(Activity activity) {
        super(activity);
    }

 Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
           /* if (isLoadMore) {//是加载更多
                page++;
                loadData(page);
                isLoadMore = false;
                // 关闭加载更多的效果
                mXListView.stopLoadMore();
            } else {//是下拉刷新
                mSmileBeanList.clear();
                page = 0;
                loadData(page);
                // 刷新listview
                mSmileAdapter.notifyDataSetChanged();
                //关闭下拉刷新的效果
                mXListView.stopRefresh();
            }*/
            if (isLoadMore) {//是加载更多
                // 关闭加载更多的效果
                mXListView.stopLoadMore();
            }else{
                //关闭下拉刷新的效果
                mXListView.stopRefresh();
            }
            mSmileAdapter.notifyDataSetChanged();

        }
    };

    @Override
    public void initview() {
        mView = View.inflate(mActivity, R.layout.smile_layout, null);
        mXListView = (XListView) mView.findViewById(R.id.xl_content);
    }

    @Override
    public void initData() {
        flContent.removeAllViews();
        mSmileBeanList.clear();

        String address = SMILE_QUEUE + page + "&pagesize=10&sort=asc&time=1418745334";
        HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                System.out.println("response---" + response);
                parseData(response);
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mSmileAdapter = new SmileAdapter();
                        mXListView.setAdapter(mSmileAdapter);
                        //是否支持下拉刷新，默认是true
                        mXListView.setPullRefreshEnable(true);
                        //是否支持上拉加载更多,默认是false
                        mXListView.setPullLoadEnable(true);
/*
                    for(int i = 0;i<mSmileBeanList.size();i++){
                        SmileBean.ResultEntity.DataEntity dataEntity = mSmileBeanList.get(i);
                        System.out.println(dataEntity.getContent());
                        System.out.println(dataEntity.getUpdatetime());
                    }*/
                    }
                });
            }

            @Override
            public void onError(Exception e) {

            }
        });


        //向FrameLayout中动态添加布局
        flContent.addView(mView);
    }


    @Override
    public void initListener() {
        mXListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                /**
                 * 下拉刷新
                 */
                //设置刷新的时间
                if (mPreUpdateTime != 0) {
                    mXListView.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(mPreUpdateTime)));
                }
                mPreUpdateTime = System.currentTimeMillis();
                mSmileBeanList.clear();
                page = 0;
                isLoadMore=false;
                loadData(page);


            }

            @Override
            public void onLoadMore() {
                /**
                 * 上拉加载更多
                 */
                ++page;
                isLoadMore = true;
                loadData(page);

            }
        });
    }

    class SmileAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (mSmileBeanList != null) {
                return mSmileBeanList.size();
            }
            return 0;
        }

        @Override
        public SmileBean.ResultEntity.DataEntity getItem(int position) {
            return mSmileBeanList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(mActivity, R.layout.smile_item, null);
                holder.content = (TextView) convertView.findViewById(R.id.content);
                holder.time = (TextView) convertView.findViewById(R.id.updatetime);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            SmileBean.ResultEntity.DataEntity dataEntity = mSmileBeanList.get(position);
            String content = dataEntity.getContent();
            String updatetime = dataEntity.getUpdatetime();
            holder.content.setText(content);
            holder.time.setText(updatetime);
            return convertView;

        }
    }

    /**
     * 解析数据
     */
    private void parseData(String response) {
        Gson gson = new Gson();
        SmileBean smileBean = new SmileBean();
        smileBean = gson.fromJson(response, SmileBean.class);
        for (int i = 0; i < 10; i++) {
            SmileBean.ResultEntity.DataEntity dataEntity = smileBean.getResult().getData().get(i);
            mSmileBeanList.add(dataEntity);

        }


    }

    static class ViewHolder {
        TextView content;
        TextView time;
    }

    /**
     * 加载更多数据
     */
    private void loadData(int page) {
        String address = SMILE_QUEUE + page + "&pagesize=10&sort=asc&time=1418745334";
        HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                Gson gson = new Gson();
                SmileBean smileBean = new SmileBean();
                smileBean = gson.fromJson(response, SmileBean.class);
                for (int i = 0; i < 10; i++) {
                    SmileBean.ResultEntity.DataEntity dataEntity = smileBean.getResult().getData().get(i);
                    mSmileBeanList.add(dataEntity);
                }
                mHandler.sendMessageDelayed(Message.obtain(), 2000);
            }

            @Override
            public void onError(Exception e) {

            }
        });
    }
}
